#include <iostream>
#include <string>
#include <memory>
class Fruit
{

public:
    virtual void getname() = 0;
};

class Banana : public Fruit
{
public:
    void getname() override
    {
        std::cout << "我是一个香蕉" << std::endl;
    }
};

class Apple : public Fruit
{
public:
    void getname() override
    {
        std::cout << "我是一个苹果" << std::endl;
    }
};
//动物；
class Animal
{

public:
    virtual void getname() = 0;
};

class  Sheep: public Animal
{
public:
    void getname() override
    {
        std::cout << "我是一只绵羊" << std::endl;
    }
};

class  Cow: public Animal
{
public:
    void getname() override
    {
        std::cout << "我是一只奶牛" << std::endl;
    }
};

class Factory
{
   public:
   Factory(){}
       virtual std::shared_ptr<Fruit>create_Fruit(const std::string& name)=0;
       virtual std::shared_ptr<Animal>create_Animal(const std::string& name)=0;



};

class FruitFactory:public Factory{


    public:
  
        std::shared_ptr<Fruit>create_Fruit(const std::string& name)override{
              if(name=="苹果") return std::shared_ptr<Apple>(new Apple());
                else if(name=="香蕉") return std::shared_ptr<Banana>(new Banana());
                else { return std::shared_ptr<Fruit>();}
             
        }

       std::shared_ptr<Animal>create_Animal(const std::string& name){
        return std::shared_ptr<Animal>();
       }
     
};

class AnimalFactory:public Factory{


    public:
    
      std::shared_ptr<Animal>create_Animal(const std::string& name)override{
              if(name=="奶牛") return std::shared_ptr<Cow>(new Cow());
                else if(name=="绵羊") return std::shared_ptr<Sheep>(new Sheep());
              else { return  std::shared_ptr<Animal>();}
        }

       std::shared_ptr<Fruit>create_Fruit(const std::string& name){
        return std::shared_ptr<Fruit>();
       }
     
};

class FactoryBuilder{
  public:
    std:: shared_ptr<Factory> build(const std::string & type){
        if(type=="动物"){
            return std::shared_ptr<AnimalFactory>(new AnimalFactory ());
        }
        else if(type=="水果") {
            return std::shared_ptr<FruitFactory>(new FruitFactory ());
        }
     
        else {  return std:: shared_ptr<Factory>();}

        }
     
};
int main()
{    
    std::shared_ptr<FactoryBuilder> fb(new FactoryBuilder());
   std::shared_ptr<Factory>ff =fb->build("水果");
   ff->create_Fruit("苹果")->getname();
   ff->create_Fruit("香蕉")->getname();

   std::shared_ptr<Factory>af =fb->build("动物");
   af->create_Animal("奶牛")->getname();
   af->create_Animal("绵羊")->getname();


     
   
    return 0;
}
